/*  
* Copyright (c) 2010, James Daniello
* This project is licensed under the 'New BSD' license.
* The full text of the license can be found in the root
* directory under the filename "LICENSE"
*/
package src.gui;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PICAIUClientGUIResetPass2 implements PICAIUGUI{

  //The various gui components.
  private Container frame;       //<-- the main panel container
  private JPanel content;
  private PICAIUClientGUI parent;
  
  private JLabel emailAdd = new JLabel("");
  private JPasswordField password;
  private JPasswordField passwordConf;
  
  private JTextField verCode;
  private JButton recover, cancel;
  private JLabel status;
  
  
  /**
  * Creates the gui component for when adding a server
  */
  public PICAIUClientGUIResetPass2(Container frameIn, PICAIUClientGUI parentIn){
    frame = frameIn;
    parent = parentIn;
    initGUI();
  }
  
  
  /**
  * creates the gui components
  */
  public void initGUI(){
  
    content = new JPanel();
    
    JPanel padding1 = new JPanel();
    padding1.setSize(750,55);
    padding1.setPreferredSize(new java.awt.Dimension(750,55));
    padding1.setMaximumSize(new java.awt.Dimension(750,55));
    padding1.setMinimumSize(new java.awt.Dimension(750,55));
    content.add(padding1);
    
    JLabel logo = new JLabel("                         Recover Info/Reset Password                      ");
    Font f = new Font("Dialog", Font.PLAIN, 32);
    logo.setFont(f);
    content.add(logo);
    
    JPanel padding2 = new JPanel();
    padding2.setSize(750,45);
    padding2.setPreferredSize(new java.awt.Dimension(750,45));
    padding2.setMaximumSize(new java.awt.Dimension(750,45));
    padding2.setMinimumSize(new java.awt.Dimension(750,45));
    content.add(padding2);
      
      
    JPanel inputPanel = new JPanel(new GridBagLayout());
    GridBagConstraints con = new GridBagConstraints();
    
    con.gridx=0;
    con.gridy=0;
    con.gridwidth=2;
  
    verCode = new JTextField();
    verCode.setColumns(28);
  
    inputPanel.add(new JLabel("An email has been sent to: "),con);
    con.gridy=1;
    inputPanel.add(emailAdd,con);
    con.gridy=2;
    inputPanel.add(new JLabel("You may have to wait several minutes for the message to arrive."),con);
    con.gridy=3;
    inputPanel.add(new JLabel("  "),con);
    con.gridy=4;
    con.gridx=0;
    con.gridwidth=1;
    inputPanel.add(new JLabel("Verification Code:  "),con);
    con.gridx=1;
    inputPanel.add(verCode,con); 
    con.gridx=0;
    con.gridy=5;
    inputPanel.add(new JLabel("  "),con);
    con.gridx=0;
    con.gridy=6;
    inputPanel.add(new JLabel("Password:              "),con);
    password = new JPasswordField();
    password.setColumns(28);
    con.gridx=1;
    inputPanel.add(password,con);
    con.gridx=0;
    con.gridy=7;
    inputPanel.add(new JLabel("Confirm Pass:        "),con);
    passwordConf = new JPasswordField();
    passwordConf.setColumns(28);
    con.gridx=1;
    inputPanel.add(passwordConf,con);
  
    con.gridy=8;
    inputPanel.add(new JLabel(" "),con);
    recover = new JButton("Recover");
    recover.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
      
      
        Pattern p = Pattern.compile("[^a-zA-Z0-9_\\.]");
        Matcher m = p.matcher(new String(password.getPassword()));
        
        if(String.valueOf(password.getPassword()).equals("")||String.valueOf(passwordConf.getPassword()).equals("")){
          status.setText("     Incorrect password     ");
          return;
        }
        if(!String.valueOf(password.getPassword()).equals(String.valueOf(passwordConf.getPassword()))){
          status.setText(" Passwords must match ");
          return;
        }
        
        m = p.matcher(new String(password.getPassword()));
        if(m.find()){
          status.setText("Password has bad chars");
          return;
        }
        
        
        recover.setEnabled(false);
        cancel.setEnabled(false);
        String passNow = parent.getClient().md5(new String(password.getPassword()));
        parent.getClient().sendDataToServer("VER#"+verCode.getText().trim()+"#"+passNow+"#"+emailAdd.getText());
        passNow="            ";
        passNow=null;
      }
    });
    cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
        parent.setGUI("login old");
      }
    });
  
    con.gridwidth=2;
    con.gridy=9;
  JPanel stat= new JPanel();
    status = new JLabel("                                               ");
    status.setForeground(Color.red);
  stat.add(status);
    stat.add(recover);
    stat.add(cancel);
  
  inputPanel.add(stat,con);
  
    content.add(inputPanel);
  }
  
  
  /**
  * Indicates that a recover of username failed
  */
  public void changeFailed(String error){
    recover.setEnabled(true);
    cancel.setEnabled(true);
    status.setText(error);
  }
  
  public void setResetPassword2Email(String emailIn){
    emailAdd.setText(emailIn);
  }
  
  /**
  * This method switches the clients main container to this GUI's graphics.
  */
  @Override
  public void setAsGUI(){
    frame.removeAll();
    frame.add(content,BorderLayout.CENTER);
    verCode.requestFocusInWindow();
    frame.validate();
  
  }

  /**
  * This sets the colors of all the components affected by the color scheme to that of the current 
  * colors for the GUI
  */
  @Override
  public void setColors(PICAIUGUIScheme colorScheme){
    
    Color colorBG01=colorScheme.getBG01();
    Color colorBG02=colorScheme.getBG02();
    Color colorFG01=colorScheme.getFG01();
    Color colorBorder01=colorScheme.getBorder01();
  
  }

}